package utils.queue
{
import flash.events.Event;
import flash.events.EventDispatcher;

[Event(name="interrupt", type="utils.queen.QueueEvent")]
[Event(name="resume", type="utils.queen.QueueEvent")]
[Event(name="cancel", type="utils.queen.QueueEvent")]
[Event(name="cancelAll", type="utils.queen.QueueEvent")]
[Event(name="complete", type="utils.queen.QueueEvent")]
/**
 * Queue
 * @author jliang
 * 
 */
public class Queue extends EventDispatcher
{	
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function Queue()
	{
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	private var _index:int = 0;	

	public function get index():int
	{
		return _index;
	}

	public function set index(value:int):void
	{
		_index = value;
	}
	
	private var _isCompleted:Boolean = false;

	public function get isCompleted():Boolean
	{
		return _isCompleted;
	}
	
	private var _isWorking:Boolean = false;
	
	public function get isWorking():Boolean
	{
		return _isWorking;
	}

	private var _data:*;

	public function get data():*
	{
		return _data;
	}

	public function set data(value:*):void
	{
		_data = value;
		if (handler)
		{
			handler.data = data;
		}
	}
	
	private var _handler:IQueueHandler;

	public function get handler():IQueueHandler
	{
		return _handler;
	}

	public function set handler(value:IQueueHandler):void
	{
		_handler = value;
		_handler.queue = this;
		if (data)
		{
			_handler.data = data;
		}
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function start():void
	{
		_isWorking = true;
		if (handler)
		{
			handler.start();
		}
		else
		{
			complete();
		}
	}
	
	public function interrupt():void
	{
		this.dispatchEvent(new QueueEvent(QueueEvent.INTERRUPT));
	}
	
	public function resume():void
	{
		this.dispatchEvent(new QueueEvent(QueueEvent.RESUME));
	}
	
	public function complete():void
	{
		_isCompleted = true;
		_isWorking = false;
		this.dispatchEvent(new QueueEvent(QueueEvent.COMPLETE));
	}
	
	public function cancel():void
	{
		this.dispatchEvent(new QueueEvent(QueueEvent.CANCEL));
	}
	
	public function cancelHandler():void
	{
		if (handler)
		{
			handler.cancel();
		}
	}
	
	public function cancelAll():void
	{
		this.dispatchEvent(new QueueEvent(QueueEvent.CANCEL_ALL));
	}
	
	public function dipose():void
	{
		if (handler)
		{
			handler.dispose();
		}
	}
}
}



